// 基础 SEO 配置
export const defaultSEO = {
    titleTemplate: '%s | 在线直播平台',
    defaultTitle: '在线直播平台',
    description: '最受欢迎的在线直播平台，提供游戏、娱乐、音乐、电竞等多种直播内容',
    openGraph: {
        type: 'website',
        locale: 'zh_CN',
        url: 'https://your-domain.com',
        siteName: '在线直播平台',
        images: [
            {
                url: 'https://your-domain.com/og-image.jpg',
                width: 1200,
                height: 630,
                alt: '在线直播平台',
            },
        ],
    },
};

// SEO 规则检查器
export function validateSEOConfig(config) {
    const rules = [
        {
            field: 'title',
            test: value => value && value.length <= 60,
            message: 'Title should exist and be less than 60 characters',
        },
        {
            field: 'description',
            test: value => value && value.length <= 160,
            message: 'Description should exist and be less than 160 characters',
        },
        {
            field: 'keywords',
            test: value => !value || value.split(',').length <= 10,
            message: 'Keywords should not exceed 10 items',
        },
    ];

    const errors = [];
    rules.forEach(rule => {
        if (!rule.test(config[rule.field])) {
            errors.push(rule.message);
        }
    });

    return errors;
}

// 生成结构化数据
export function generateStructuredData(type, data) {
    const baseStructuredData = {
        '@context': 'https://schema.org',
        '@type': type,
    };

    switch (type) {
        case 'LiveStream':
            return {
                ...baseStructuredData,
                name: data.title,
                description: data.description,
                broadcaster: {
                    '@type': 'Person',
                    name: data.streamerName,
                },
                thumbnail: data.thumbnail,
                uploadDate: data.startTime,
                duration: data.duration,
                interactionStatistic: {
                    '@type': 'InteractionCounter',
                    interactionType: { '@type': 'WatchAction' },
                    userInteractionCount: data.viewerCount,
                },
            };
        case 'BreadcrumbList':
            return {
                ...baseStructuredData,
                itemListElement: data.map((item, index) => ({
                    '@type': 'ListItem',
                    position: index + 1,
                    item: {
                        '@id': item.url,
                        name: item.name,
                    },
                })),
            };
        default:
            return baseStructuredData;
    }
}

// SEO 最佳实践指南
export const seoGuidelines = {
    // 页面命名规范
    pageNaming: {
        // 使用 generateMetadata 而不是静态 metadata
        useGenerateMetadata: true,
        // 动态路由页面必须实现 generateStaticParams
        requireGenerateStaticParams: true,
    },

    // 性能优化
    performance: {
        // 默认使用 SSG
        preferSSG: true,
        // 需要实时数据时使用 ISR
        useISR: {
            revalidateInterval: 60, // 默认 60 秒
        },
        // 仅在必要时使用 SSR
        useSSR: false,
    },

    // 图片优化
    images: {
        // 必须提供 alt 文本
        requireAlt: true,
        // 使用响应式图片
        responsive: {
            sizes: [640, 750, 828, 1080, 1200],
            breakpoints: {
                sm: 640,
                md: 750,
                lg: 828,
                xl: 1080,
                '2xl': 1200,
            },
        },
    },

    // 链接优化
    links: {
        // 内部链接使用 Next.js Link 组件
        useNextLink: true,
        // 外部链接添加 rel="noopener noreferrer"
        externalRelAttributes: ['noopener', 'noreferrer'],
    },
};

// 辅助函数：生成面包屑导航数据
export function generateBreadcrumbs(path) {
    const parts = path.split('/').filter(Boolean);
    const breadcrumbs = [{ name: '首页', url: '/' }];

    let currentPath = '';
    parts.forEach(part => {
        currentPath += `/${part}`;
        breadcrumbs.push({
            name: part.charAt(0).toUpperCase() + part.slice(1),
            url: currentPath,
        });
    });

    return breadcrumbs;
}

// 辅助函数：生成规范的 URL
export function generateCanonicalUrl(path) {
    const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || 'https://your-domain.com';
    return `${baseUrl}${path}`;
}
